﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using ENail.Domain.Core;
using ENail.Domain.Core.Contract.Services;
using ENail.Portal.Web.Models;
using Equation.Enterprise.Utilities.DesignByContract;

namespace ENail.Portal.Web.Controllers
{
    public class EmployeeController : DUALController<EmployeeListViewItem,
                                                    EmployeeViewModel,
                                                    Employee>
    {
        private readonly IEmployeeService _employeeService ;

        public EmployeeController(IEmployeeService employeeService) : base("List", "Detail", new EmployeeListViewModelMapper(), new EmployeeViewModelMapper())
        {
            Check.Require(employeeService != null);

            this._employeeService = employeeService;
        }

        protected override List<Employee> GetList()
        {
            return _employeeService.GetAllActiveEmployees().ToList();
        }

        protected override Employee Get(int id)
        {
            return _employeeService.GetEmployee(id);
        }

        protected override void Add(Employee domainModel)
        {
            var user = User as User;
            domainModel.NailSalon = user.NailSalon;

            _employeeService.InsertEmployee(domainModel);
        }

        protected override void Update(Employee domainModel)
        {
            _employeeService.UpdateEmployee(domainModel);
        }

        protected override void Delete(Employee domainModel)
        {
            _employeeService.InActiveEmployee(domainModel);
        }

        //[HttpGet]
        //public ActionResult Index()
        //{
        //    var employees = new EmployeeListViewModel();
        //    employees.MapFromListDomainModel(_employeeService.GetAllEmployees());
        //    return View(employees);
        //}

        //[HttpGet]
        //public ActionResult Update(int id)
        //{
        //    var employee = new EmployeeViewModel();
        //    employee.MapFromDomainModel(_employeeService.GetEmployee(id));

        //    return View("Details", employee);
        //}

        //[HttpPost]
        //public ActionResult Update(int id, EmployeeViewModel model)
        //{
        //    if (!ModelState.IsValid)
        //        return Update(id);

        //    var employee = _employeeService.GetEmployee(id);
        //    if (employee != null)
        //        model.MapToDomainModel(employee);
        //    _employeeService.UpdateEmployee(employee);

        //    return RedirectToAction("Index");
        //}

        //[HttpGet]
        //public ActionResult Add()
        //{
        //    var model = new EmployeeViewModel();
        //    return View("Details", model);
        //}

        //[HttpPost]
        //public ActionResult Add(EmployeeViewModel employeeViewModel)
        //{
        //    if (!ModelState.IsValid)
        //        return Add();

        //    var employee = new Employee();
        //    employeeViewModel.MapToDomainModel(employee);
        //    var user = (User) User;
        //    employee.NailSalon = user.NailSalon;
        //    _employeeService.InsertEmployee(employee);

        //    return RedirectToAction("Index");
        //}

        //[HttpPost]
        //public ActionResult Delete(int id)
        //{
        //    var domain = _employeeService.GetEmployee(id);
        //    _employeeService.InsertEmployee(domain);

        //    return View("Index");
        //}

        //DEPRECATED
        //private EmployeeViewModel GetEmployee(int id)
        //{
        //    var domainEmployee = _employeeService.GetEmployee(id);

        //    if (domainEmployee!=null)
        //    {
        //        var employee = new EmployeeViewModel();
        //        employee.MapFromDomainModel(domainEmployee);
        //        return employee;
        //    }

        //    return null;
        //}

        //DEPRECATED
        //private IList<EmployeeViewModel> GetAllEmployees()
        //{
        //    List<EmployeeViewModel> employees = _employeeService.GetAllEmployees().Select<Employee, EmployeeViewModel>
        //        (x =>
        //             {
        //                 var employee = new EmployeeViewModel();
        //                 employee.MapFromDomainModel(x);

        //                 return employee;
        //             }
        //        ).ToList();

        //    return employees ?? new List<EmployeeViewModel>();
        //}

    }
}
